package com.gwd.task.controller;

import com.gwd.task.group.GroupInsert;
import com.gwd.task.group.GroupUpdate;
import com.gwd.task.model.GroupDTO;
import com.gwd.task.model.PageUtils;
import com.gwd.task.services.GroupServer;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhiyongming
 * @date 2020/11/11 10:06
 */
@RestController
@RequestMapping(value = "/v1/group")
@SuppressWarnings("all")
public class GroupController {

    @Resource
    private GroupServer groupServer;

    /**
     * 查询所有
     * @return List<GroupDTO>
     */
    @GetMapping(value = "/findGroup")
    public List<GroupDTO> findGroup(@RequestAttribute("userId") Long id) {
        return groupServer.findAll(id);
    }

    /**
     * 按分页查询
     * @param dto
     * @param pageUtils
     * @return
     */
    @GetMapping(value = "/findGroupByPage")
    public PageUtils findGroupByPage(GroupDTO dto, @Validated PageUtils pageUtils) {
        return groupServer.findByPage(dto, pageUtils);
    }

    /**
     * 新增
     * @param GroupDTO
     */
    @PostMapping(value = "/add")
    public void add(@RequestBody @Validated({GroupInsert.class}) GroupDTO dto) {
        groupServer.save(dto);
    }

    /**
     * 修改
     * @param GroupDTO
     */
    @PutMapping(value = "/edit")
    public void edit(@RequestBody @Validated({GroupUpdate.class}) GroupDTO dto) {
        groupServer.save(dto);
    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping(value = "/remove/{id}")
    public void remove(@PathVariable("id") Long id) {
        groupServer.remove(id);
    }

    @PutMapping(value = "/active/{id}")
    public void active(@PathVariable("id") Long id) {
        groupServer.active(id);
    }

}
